package com.fanyue.modules.wzb.service.impl;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.fanyue.core.base.BaseServiceImpl;
import com.fanyue.core.cache.CoolCache;
import com.fanyue.core.exception.CoolPreconditions;
import com.fanyue.core.request.R;
import com.fanyue.core.util.CoolSecurityUtil;
import com.fanyue.core.util.DatabaseDialectUtils;
import com.fanyue.modules.base.entity.sys.BaseSysParamEntity;
import com.fanyue.modules.base.entity.sys.BaseSysUserEntity;
import com.fanyue.modules.base.service.sys.BaseSysDepartmentService;
import com.fanyue.modules.base.service.sys.BaseSysParamService;
import com.fanyue.modules.base.service.sys.BaseSysUserService;
import com.fanyue.modules.wzb.entity.*;
import com.fanyue.modules.wzb.mapper.AppUserFaceRecordMapper;
import com.fanyue.modules.wzb.service.*;
import com.fanyue.modules.wzb.utils.CodeUtils;
import com.fanyue.modules.wzb.vo.AppUserFaceRecordVO;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.tangzc.autotable.core.constants.DatabaseDialect;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.fanyue.modules.base.entity.sys.table.BaseSysDepartmentEntityTableDef.BASE_SYS_DEPARTMENT_ENTITY;
import static com.fanyue.modules.wzb.entity.table.AppUserEntityTableDef.APP_USER_ENTITY;
import static com.fanyue.modules.wzb.entity.table.AppUserFaceRecordEntityTableDef.APP_USER_FACE_RECORD_ENTITY;
import static com.fanyue.modules.wzb.entity.table.CargoEntityTableDef.CARGO_ENTITY;
import static com.fanyue.modules.wzb.entity.table.ClothingInfoEntityTableDef.CLOTHING_INFO_ENTITY;


/**
 * 系统用户
 */
@Service
@RequiredArgsConstructor
public class AppUserFaceRecordServiceImpl extends BaseServiceImpl<AppUserFaceRecordMapper, AppUserFaceRecordEntity> implements AppUserFaceRecordService {

    final private CoolCache coolCache;
    final private BaseSysUserService sysUserService;
    final private AppUserService appUserService;
    final private CargoService cargoService;
    final private ClothingInfoService clothingInfoService;
    final private ClothingRecordService clothingRecordService;
    final private ClothingBatchService clothingBatchService;
    final private BaseSysParamService sysParamService;
    final private BaseSysDepartmentService sysDepartmentService;
    final private ClothingBatchItemService clothingBatchItemService;

    @Override
    public Object page(JSONObject requestParams, Page<AppUserFaceRecordEntity> page, QueryWrapper qw) {
        String dateStartTime = requestParams.getStr("dateStartTime");
        String dateEndTime = requestParams.getStr("dateEndTime");
        String keyWord = requestParams.getStr("keyWord");
        Integer status = requestParams.getInt("status");
        Integer isSubmit = requestParams.getInt("isSubmit");
        Long[] departmentIds = requestParams.get("departmentIds", Long[].class);
        JSONObject tokenInfo = CoolSecurityUtil.getAdminUserInfo(requestParams);
        // 用户的部门权限
        Long[] permsDepartmentArr = coolCache.get("admin:department:" + tokenInfo.get("userId"),
                Long[].class);
        String databaseDialect = DatabaseDialectUtils.getDatabaseDialect();
        if (databaseDialect.equals(DatabaseDialect.PostgreSQL)) {
            // 兼容postgresql
            qw.select("base_sys_department.name AS departmentName");
        } else {
            qw.select(APP_USER_FACE_RECORD_ENTITY.ALL_COLUMNS,
                    APP_USER_ENTITY.NAME.as("appUserName"),
                    APP_USER_ENTITY.UID.as("appUserUid"),
                    BASE_SYS_DEPARTMENT_ENTITY.NAME.as("departmentName"));
        }

        qw.from(APP_USER_FACE_RECORD_ENTITY).leftJoin(BASE_SYS_DEPARTMENT_ENTITY)
                .on(APP_USER_FACE_RECORD_ENTITY.DEPARTMENT_ID.eq(BASE_SYS_DEPARTMENT_ENTITY.ID))
                .leftJoin(APP_USER_ENTITY)
                .on(APP_USER_FACE_RECORD_ENTITY.USER_ID.eq(APP_USER_ENTITY.ID));

        // 筛选部门
        qw.and(APP_USER_FACE_RECORD_ENTITY.DEPARTMENT_ID.in(departmentIds,
                ArrayUtil.isNotEmpty(departmentIds)));
        // 筛选状态
        qw.and(APP_USER_FACE_RECORD_ENTITY.STATUS.eq(status, status != null));
        qw.and(APP_USER_FACE_RECORD_ENTITY.IS_SUBMIT.eq(isSubmit, isSubmit != null));
        // 搜索关键字
        if (StrUtil.isNotEmpty(keyWord)) {
            qw.and(APP_USER_ENTITY.NAME.like(keyWord)
                    .or(APP_USER_ENTITY.UID.like(keyWord)));
        }

        if (StrUtil.isNotEmpty(dateStartTime)) {
            qw.and(APP_USER_FACE_RECORD_ENTITY.CREATE_TIME.ge(dateStartTime));
        }
        if (StrUtil.isNotEmpty(dateEndTime)) {
            qw.and(APP_USER_FACE_RECORD_ENTITY.CREATE_TIME.le(dateEndTime));
        }

        // 过滤部门权限
        qw.and(APP_USER_FACE_RECORD_ENTITY.DEPARTMENT_ID.in(
                permsDepartmentArr == null || permsDepartmentArr.length == 0 ? new Long[]{null}
                        : permsDepartmentArr,
                !CoolSecurityUtil.getAdminUsername().equals("admin")));
        if (databaseDialect.equals(DatabaseDialect.PostgreSQL)) {
            // 兼容postgresql
            qw.groupBy("base_sys_department.name");
        } else {
            qw.groupBy(APP_USER_FACE_RECORD_ENTITY.ID);
        }
        return mapper.paginate(page, qw);
    }


    @Override
    public List<AppUserFaceRecordVO> detailList(JSONObject requestParams) {
        Integer status = requestParams.getInt("status");
        Integer type = requestParams.getInt("type");
        QueryWrapper queryWrapper = new QueryWrapper();
        JSONObject tokenInfo = CoolSecurityUtil.getAdminUserInfo(requestParams);
        // 用户的部门权限
        Long[] permsDepartmentArr = coolCache.get("admin:department:" + tokenInfo.get("userId"),
                Long[].class);
        queryWrapper.select(APP_USER_FACE_RECORD_ENTITY.ALL_COLUMNS,
                APP_USER_ENTITY.NAME.as("appUserName"),
                APP_USER_ENTITY.UID.as("appUserUid"));
        queryWrapper.from(APP_USER_FACE_RECORD_ENTITY).leftJoin(APP_USER_ENTITY)
                .on(APP_USER_FACE_RECORD_ENTITY.USER_ID.eq(APP_USER_ENTITY.ID));
        queryWrapper.and(APP_USER_FACE_RECORD_ENTITY.STATUS.eq(status, status != null));
        queryWrapper.and(APP_USER_FACE_RECORD_ENTITY.TYPE.eq(type, type != null));
        queryWrapper.and(APP_USER_FACE_RECORD_ENTITY.DEPARTMENT_ID.in(
                permsDepartmentArr == null || permsDepartmentArr.length == 0 ? new Long[]{null}
                        : permsDepartmentArr,
                !CoolSecurityUtil.getAdminUsername().equals("admin")));
        queryWrapper.groupBy(APP_USER_FACE_RECORD_ENTITY.ID);
        queryWrapper.orderBy(APP_USER_FACE_RECORD_ENTITY.UPDATE_TIME.desc());
        //查询基本信息
        List<AppUserFaceRecordEntity> list = mapper.selectListByQuery(queryWrapper);
        //查询当前占用的货架和被服
        CargoEntity selectCargo = new CargoEntity();
        selectCargo.setStatus(9);
        //selectCargo.setDepartmentIds(permsDepartmentArr);
        List<CargoEntity> cargoList = cargoService.listByInfo(selectCargo);
        ClothingInfoEntity selectClothing = new ClothingInfoEntity();
        selectClothing.setStatus(9);
        //selectClothing.setDepartmentIds(permsDepartmentArr);
        List<ClothingInfoEntity> clothingList = clothingInfoService.listByInfo(selectClothing);
        List<AppUserFaceRecordVO> listVo = new ArrayList<>();
        for (AppUserFaceRecordEntity record: list) {
            AppUserFaceRecordVO recordVO = new AppUserFaceRecordVO();
            recordVO.setId(record.getId());
            recordVO.setType(record.getType());
            recordVO.setStatus(record.getStatus());
            recordVO.setAppUserName(record.getAppUserName());
            recordVO.setIsSubmit(record.getIsSubmit());
            String uid = "";
            //显示后六位
            if (record.getAppUserUid() != null) {
                if (record.getAppUserUid().length() > 6) {
                    int length = record.getAppUserUid().length();
                    uid = record.getAppUserUid().substring(length - 6);
                } else {
                    uid = record.getAppUserUid();
                }
            }
            recordVO.setAppUserUid(uid);
            recordVO.setCreateTime(record.getCreateTime());
            recordVO.setCargo1("");
            recordVO.setCargo2("");
            recordVO.setCargo3("");
            recordVO.setCargo4("");
            //1-借出,2-归还
            if (record.getType() == 1) {
                for (ClothingInfoEntity clothing: clothingList) {
                    if (clothing.getFaceRecordId().equals(record.getId())) {
                        if (clothing.getCategory().equals(1)) {
                            recordVO.setCargo1(clothing.getCargoPosition());
                        }
                        if (clothing.getCategory().equals(2)) {
                            recordVO.setCargo2(clothing.getCargoPosition());
                        }
                        if (clothing.getCategory().equals(3)) {
                            recordVO.setCargo3(clothing.getCargoPosition());
                        }
                        if (clothing.getCategory().equals(4)) {
                            recordVO.setCargo4(clothing.getCargoPosition());
                        }
                    }
                }
            }
            if (record.getType() == 2) {
                for (CargoEntity cargo: cargoList) {
                    if (cargo.getFaceRecordId().equals(record.getId())) {
                        if (cargo.getCategory().equals(1)) {
                            recordVO.setCargo1(cargo.getPosition());
                        }
                        if (cargo.getCategory().equals(2)) {
                            recordVO.setCargo2(cargo.getPosition());
                        }
                        if (cargo.getCategory().equals(3)) {
                            recordVO.setCargo3(cargo.getPosition());
                        }
                        if (cargo.getCategory().equals(4)) {
                            recordVO.setCargo4(cargo.getPosition());
                        }
                    }
                }
            }
            listVo.add(recordVO);
        }
        return listVo;
    }

    @Override
    public List<AppUserFaceRecordEntity> listByInfo(AppUserFaceRecordEntity selectRecord) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(APP_USER_FACE_RECORD_ENTITY.ALL_COLUMNS);
        queryWrapper.from(APP_USER_FACE_RECORD_ENTITY);
        queryWrapper.and(APP_USER_FACE_RECORD_ENTITY.STATUS.eq(selectRecord.getStatus(), selectRecord.getStatus() != null));
        queryWrapper.and(APP_USER_FACE_RECORD_ENTITY.USER_ID.eq(selectRecord.getUserId(), selectRecord.getUserId() != null));
        queryWrapper.and(APP_USER_FACE_RECORD_ENTITY.UPDATE_TIME.le(selectRecord.getUpdateTime(), selectRecord.getUpdateTime() != null));
        return mapper.selectListByQuery(queryWrapper);
    }

    @Override
    public void cancel(List<AppUserFaceRecordEntity> recordEntityList) {
        List<CargoEntity> cargoList = new ArrayList<>();
        List<ClothingInfoEntity> clothingList = new ArrayList<>();
        List<ClothingRecordEntity> clothingRecordList = new ArrayList<>();
        List<ClothingBatchItemEntity> itemList = new ArrayList<>();
        for (AppUserFaceRecordEntity record: recordEntityList) {
            record.setStatus(1);

            //如果是借出，进行解锁，归还，是否已经提交数据，没有就进行解锁
            if (record.getType() == 1 || (record.getType() == 2 && record.getIsSubmit() == 0)) {
                CargoEntity selectCargo = new CargoEntity();
                selectCargo.setStatus(9);
                selectCargo.setFaceRecordId(record.getId());
                List<CargoEntity> cargoEntityList = cargoService.listByInfo(selectCargo);
                for (CargoEntity cargo: cargoEntityList) {
                    cargo.setStatus(record.getType() == 1 ? 1:0); //借出失败，说明货架有东西，归还失败说明没有东西
                    cargoList.add(cargo);
                }
                ClothingInfoEntity selectClothing = new ClothingInfoEntity();
                selectClothing.setStatus(9);
                selectClothing.setFaceRecordId(record.getId());
                List<ClothingInfoEntity> clothingInfoList = clothingInfoService.listByInfo(selectClothing);
                for (ClothingInfoEntity info: clothingInfoList) {
                    info.setStatus(record.getType());
                    clothingList.add(info);
                }
            } else {
                BaseSysUserEntity sysUserEntity = sysUserService.getById(record.getSysUserId());
                AppUserEntity appUser = appUserService.getById(record.getUserId());
                //查询当前锁定被装, 进行归还
                ClothingInfoEntity selectClothing = new ClothingInfoEntity();
                selectClothing.setFaceRecordId(record.getId());
                selectClothing.setStatus(9);
                List<ClothingInfoEntity> clothingLockList = clothingInfoService.listByInfo(selectClothing);
                for (ClothingInfoEntity clothing: clothingLockList) {
                    CargoEntity cargoEntity = cargoService.getById(clothing.getCargoId());
                    ClothingRecordEntity clothingRecord = new ClothingRecordEntity();
                    clothingRecord.setCreateTime(new Date());
                    clothingRecord.setClothingId(clothing.getId());
                    clothingRecord.setClothingCode(clothing.getCode());
                    clothingRecord.setAppUserId(appUser.getId());
                    clothingRecord.setSysUserId(sysUserEntity.getId());
                    clothingRecord.setSysUserName(sysUserEntity.getName());
                    clothingRecord.setDepartmentId(clothing.getDepartmentId());
                    clothingRecord.setFaceRecordId(record.getId());
                    clothingRecord.setType(2);
                    clothingRecord.setStatus(1);

                    cargoEntity.setStatus(1);
                    //判断货架是否有东西，有就进行下架操作
                    ClothingInfoEntity selectClothing1 = new ClothingInfoEntity();
                    selectClothing1.setStatus(1);
                    selectClothing1.setCargoId(cargoEntity.getId());
                    List<ClothingInfoEntity> clothingList1 = clothingInfoService.listByInfo(selectClothing1);
                    if (clothingList1.size() > 0) {
                        clothing.setStatus(0);
                    } else {
                        clothingRecord.setCargoId(cargoEntity.getId());
                        clothingRecord.setCargoPosition(cargoEntity.getPosition());
                        clothing.setStatus(1);
                    }
                    cargoList.add(cargoEntity);
                    clothingList.add(clothing);
                    clothingRecordList.add(clothingRecord);

                    //查询批量信息
                    ClothingBatchItemEntity selectItem = new ClothingBatchItemEntity();
                    selectItem.setClothingId(clothing.getId());
                    selectItem.setStatus(0);
                    List<ClothingBatchItemEntity> item1List = clothingBatchItemService.listByInfo(selectItem);
                    for (ClothingBatchItemEntity item: item1List) {
                        item.setStatus(1);
                        itemList.add(item);
                    }

                }
            }
        }

        cargoService.updateBatch(cargoList);
        clothingInfoService.updateBatch(clothingList);
        clothingRecordService.saveBatch(clothingRecordList);
        updateBatch(recordEntityList);
        clothingBatchItemService.updateBatch(itemList);
    }



    @Override
    public AppUserFaceRecordVO detail(Long id) {
        AppUserFaceRecordEntity recordEntity = mapper.selectOneById(id);
        AppUserFaceRecordVO recordVO = new AppUserFaceRecordVO();

        recordVO.setId(id);
        recordVO.setType(recordEntity.getType());
        AppUserEntity appUser = appUserService.getById(recordEntity.getUserId());
        recordVO.setHeadImage(appUser.getHeadImage());
        recordVO.setAppUserUid(appUser.getUid() != null ? appUser.getUid(): "");
        recordVO.setAppUserName(appUser.getName());
        String deptPath = sysDepartmentService.deptPathById(appUser.getDepartmentId(), "");
        recordVO.setDepartmentName(deptPath);
        //1-借出,2-归还
        if (recordVO.getType() == 1) {
            List<Map<String, Object>> sureList = new ArrayList<>();
            List<Map<String, Object>> lendList = new ArrayList<>();
            //已借出
            ClothingInfoEntity selectClothing1 = new ClothingInfoEntity();
            selectClothing1.setOpUserId(recordEntity.getUserId());
            selectClothing1.setStatus(2);
            List<ClothingInfoEntity> clothingLendList = clothingInfoService.listByInfo(selectClothing1);
            //可借出
            ClothingInfoEntity selectClothing2 = new ClothingInfoEntity();
            selectClothing2.setFaceRecordId(recordEntity.getId());
            selectClothing2.setStatus(9);
            List<ClothingInfoEntity> clothingSureList = clothingInfoService.listByInfo(selectClothing2);
            for (ClothingInfoEntity clothing: clothingLendList) {
                Map<String, Object> map = new HashMap<>();
                //0-帽子、1-上衣、2-裤子、3-套装(衣服和裤子)，4-靴子
                if (clothing.getCategory() == 1) map.put("type", "作训帽");
                if (clothing.getCategory() == 2) map.put("type", "作训服");
                if (clothing.getCategory() == 3) map.put("type", "体能训练服");
                if (clothing.getCategory() == 4) map.put("type", "作战靴");
                lendList.add(map);
            }
            for (ClothingInfoEntity clothing: clothingSureList) {
                Map<String, Object> map = new HashMap<>();
                if (clothing.getCategory() == 1) map.put("type", "作训帽");
                if (clothing.getCategory() == 2) map.put("type", "作训服");
                if (clothing.getCategory() == 3) map.put("type", "体能训练服");
                if (clothing.getCategory() == 4) map.put("type", "作战靴");
                map.put("cargo", clothing.getCargoPosition());
                sureList.add(map);
            }
            recordVO.setLendList(lendList);
            recordVO.setSureList(sureList);
        }
        if (recordVO.getType() == 2) {
            List<Map<String, Object>> lendList = new ArrayList<>();
            CargoEntity selectCargo = new CargoEntity();
            selectCargo.setFaceRecordId(recordEntity.getId());
            selectCargo.setStatus(9);
            List<CargoEntity> cargoList = cargoService.listByInfo(selectCargo);
            for (CargoEntity cargo: cargoList) {
                Map<String, Object> map = new HashMap<>();
                if (cargo.getCategory() == 1) map.put("type", "作训帽");
                if (cargo.getCategory() == 2) map.put("type", "作训服");
                if (cargo.getCategory() == 3) map.put("type", "体能训练服");
                if (cargo.getCategory() == 4) map.put("type", "作战靴");
                map.put("cargo", cargo.getPosition());
                lendList.add(map);
            }
            recordVO.setLendList(lendList);
        }

        return recordVO;
    }

    @Override
    public void scheduledTask() {
        //查询当前时间
        Date date = new Date();
        AppUserFaceRecordEntity selectRecord = new AppUserFaceRecordEntity();
        selectRecord.setUpdateTime(new Date(date.getTime() - 1000 * 60 * 1));
        selectRecord.setStatus(0);
        List<AppUserFaceRecordEntity> recordEntityList = listByInfo(selectRecord);
        cancel(recordEntityList);
    }

    @Override
    public void submit(String clothingIds, Long id) {
        AppUserFaceRecordEntity faceRecord = mapper.selectOneById(id);
        faceRecord.setIsSubmit(1);
        BaseSysUserEntity sysUserEntity = sysUserService.getById(faceRecord.getSysUserId());
        ClothingInfoEntity selectClothing = new ClothingInfoEntity();
        String[] strIds = clothingIds.split(",");
        Long[] ids =new Long[strIds.length];
        for (int i = 0; i < strIds.length; i++){
            ids[i] = Long.parseLong(strIds[i]);
        }
        selectClothing.setIds(ids);
        List<ClothingInfoEntity> clothingList = clothingInfoService.listByInfo(selectClothing);
        if (faceRecord.getType() == 1) {
            AppUserEntity appUser = appUserService.getById(faceRecord.getUserId());
            List<CargoEntity> cargoEntityList = new ArrayList<>();
            List<ClothingRecordEntity> clothingRecordList = new ArrayList<>();
            for (ClothingInfoEntity clothing: clothingList) {
                CargoEntity cargoEntity = cargoService.getById(clothing.getCargoId());
                ClothingRecordEntity clothingRecord = new ClothingRecordEntity();
                clothingRecord.setCreateTime(new Date());
                clothingRecord.setClothingId(clothing.getId());
                clothingRecord.setClothingCode(clothing.getCode());
                clothingRecord.setAppUserId(appUser.getId());
                clothingRecord.setSysUserId(sysUserEntity.getId());
                clothingRecord.setSysUserName(sysUserEntity.getName());
                clothingRecord.setDepartmentId(clothing.getDepartmentId());
                clothingRecord.setFaceRecordId(faceRecord.getId());
                clothingRecord.setCargoId(cargoEntity.getId());
                clothingRecord.setCargoPosition(cargoEntity.getPosition());
                clothingRecord.setStatus(1);

                //新的
                clothingRecord.setType(1);
                clothing.setOpUserId(appUser.getId());
                clothing.setStatus(2);
                //处理货架
                cargoEntity.setStatus(0);
                cargoEntityList.add(cargoEntity);
                clothingRecordList.add(clothingRecord);
                //原来 1-借出，2-归还
                /*if (faceRecord.getType() == 1) {
                    clothingRecord.setType(1);
                    clothing.setOpUserId(appUser.getId());
                    clothing.setStatus(2);
                    //处理货架
                    cargoEntity.setStatus(0);
                    cargoEntityList.add(cargoEntity);
                    clothingRecord.setStatus(1);
                    clothingRecordList.add(clothingRecord);
                }

                if (faceRecord.getType() == 2) {
                    faceRecord.setIsSubmit(1);
                    clothingRecord.setType(2);
                    cargoEntity.setStatus(1);
                    //判断货架是否有东西，有就进行下架操作
                    ClothingInfoEntity selectClothing1 = new ClothingInfoEntity();
                    selectClothing1.setStatus(1);
                    selectClothing1.setCargoId(cargoEntity.getId());
                    List<ClothingInfoEntity> clothingList1 = clothingInfoService.listByInfo(selectClothing1);
                    if (clothingList1.size() > 0) {
                        clothing.setStatus(0);
                    } else {
                        clothing.setStatus(1);
                    }
                    cargoEntityList.add(cargoEntity);
                }
                */
            }
            //更新信息
            cargoService.updateBatch(cargoEntityList);
            clothingInfoService.updateBatch(clothingList);
            clothingRecordService.saveBatch(clothingRecordList);
            //关闭没有操作的
            List<AppUserFaceRecordEntity> faceRecordEntityList = new ArrayList<>();
            faceRecordEntityList.add(faceRecord);
            cancel(faceRecordEntityList);
        }
        if (faceRecord.getType() == 2) {
            faceRecord.setUpdateTime(new Date());
            this.updateById(faceRecord);
            //将没有归还的货架解除锁定
            //查询当前锁定货架和被装
            ClothingInfoEntity selectClothing1 = new ClothingInfoEntity();
            selectClothing1.setFaceRecordId(faceRecord.getId());
            selectClothing1.setStatus(9);
            List<ClothingInfoEntity> clothingLockList = clothingInfoService.listByInfo(selectClothing1);
            CargoEntity selectCargo = new CargoEntity();
            selectCargo.setFaceRecordId(faceRecord.getId());
            selectCargo.setStatus(9);
            List<CargoEntity> cargoLockList = cargoService.listByInfo(selectCargo);
            List<ClothingInfoEntity> clothingCancelList = new ArrayList<>();
            List<CargoEntity> cargoCancelList = new ArrayList<>();

            //剔除已提交的数据
            for (ClothingInfoEntity clothingLock: clothingLockList) {
                boolean cancel = true;
                for (ClothingInfoEntity clothing: clothingList) {
                    if (clothing.getId().equals(clothingLock.getId())) {
                        cancel = false;
                        break;
                    }
                }
                if (cancel) {
                    clothingCancelList.add(clothingLock);
                }
            }
            for (CargoEntity cargoLock: cargoLockList) {
                boolean cancel = true;
                for (ClothingInfoEntity clothing: clothingList) {
                    if (clothing.getCargoId().equals(cargoLock.getId())) {
                        cancel = false;
                        break;
                    }
                }
                if (cancel) {
                    cargoCancelList.add(cargoLock);
                }
            }
            //将剔除后的锁定数据进行解锁
            for (ClothingInfoEntity info: clothingLockList) {
                info.setStatus(2);
            }
            for (CargoEntity cargo: cargoLockList) {
                cargo.setStatus(0); //借出失败，说明货架有东西，归还失败说明没有东西
            }
            clothingInfoService.updateBatch(clothingCancelList);
            cargoService.updateBatch(cargoCancelList);
        }
    }

    @Override
    public void distribution(AppUserEntity appUser, AppUserFaceRecordEntity record) {
        //1-借出,2-归还
        if (record.getType() == 1) {
            //获取用户型号
            Long specId1 = 0L;
            Long specId2 = 0L;
            Long specId3 = 0L;
            Long specId4 = 0L;
            JSONObject jsonObject = new JSONObject(appUser.getSpecData());
            specId1 = jsonObject.getLong("spec1");
            specId2 = jsonObject.getLong("spec2");
            specId3 = jsonObject.getLong("spec3");
            specId4 = jsonObject.getLong("spec4");
            //查询当前季节
            BaseSysParamEntity baseSysParamEntity = sysParamService.getByKey("season");
            Integer season = 2;
            if (ObjectUtil.isEmpty(baseSysParamEntity)) {
                clothingInfoService.replaceSeason(season);
            } else {
                season = Integer.parseInt(baseSysParamEntity.getData());
            }
            //是否借出，0-帽子、1-上衣、2-裤子、3-套装(衣服和裤子)，4-靴子
            boolean isLend1 = false;
            boolean isLend2 = false;
            boolean isLend3 = false;
            boolean isLend4 = false;
            //查询已分配
            ClothingInfoEntity selectClothing3 = new ClothingInfoEntity();
            selectClothing3.setSeason(season);
            selectClothing3.setFaceRecordId(record.getId());
            selectClothing3.setStatus(9);
            List<ClothingInfoEntity> clothingDistributionList = clothingList(selectClothing3);
            for (ClothingInfoEntity lend: clothingDistributionList) {
                if (lend.getCategory() == 1)  isLend1 = true;
                if (lend.getCategory() == 2)  isLend2 = true;
                if (lend.getCategory() == 3)  isLend3 = true;
                if (lend.getCategory() == 4)  isLend4 = true;
            }

            //查询已经借出的
            ClothingInfoEntity selectClothing = new ClothingInfoEntity();
            selectClothing.setOpUserId(appUser.getId());
            selectClothing.setSeason(season);
            selectClothing.setDepartmentId(record.getDepartmentId());
            selectClothing.setStatus(2);
            List<ClothingInfoEntity> clothingLendList = clothingList(selectClothing);
            //是否已经借出
            for (ClothingInfoEntity lend: clothingLendList) {
                if (lend.getCategory() == 1)  isLend1 = true;
                if (lend.getCategory() == 2)  isLend2 = true;
                if (lend.getCategory() == 3)  isLend3 = true;
                if (lend.getCategory() == 4)  isLend4 = true;
            }
            //默认分配库
            ClothingInfoEntity selectClothing1 = new ClothingInfoEntity();
            selectClothing1.setIsPrivate(0);
            selectClothing1.setSeason(season);
            selectClothing1.setDepartmentId(record.getDepartmentId());
            selectClothing1.setStatus(1);
            List<ClothingInfoEntity> clothingList = clothingList(selectClothing1);
            //判断该用户类型，普通用户或缺少类型进行自动分配
            //查询私有的服装
            ClothingInfoEntity selectClothing2 = new ClothingInfoEntity();
            selectClothing2.setIsPrivate(1);
            selectClothing2.setSeason(season);
            selectClothing2.setPrUserId(appUser.getId());
            selectClothing2.setDepartmentId(record.getDepartmentId());
            selectClothing2.setStatus(1);
            List<ClothingInfoEntity> clothingPrivateList = clothingList(selectClothing2);

            //进行专属分配锁定, 不进行型号判断
            for (ClothingInfoEntity privateInfo: clothingPrivateList) {
                if (privateInfo.getCategory() == 1 && !isLend1) {
                    privateInfo.setStatus(9);
                    privateInfo.setOpUserId(record.getUserId());
                    privateInfo.setFaceRecordId(record.getId());
                    clothingInfoService.update(privateInfo);
                    isLend1 = true;
                }
                if (privateInfo.getCategory() == 2 && !isLend2) {
                    privateInfo.setStatus(9);
                    privateInfo.setOpUserId(record.getUserId());
                    privateInfo.setFaceRecordId(record.getId());
                    clothingInfoService.update(privateInfo);
                    isLend2 = true;
                }
                if (privateInfo.getCategory() == 3 && !isLend3) {
                    privateInfo.setStatus(9);
                    privateInfo.setOpUserId(record.getUserId());
                    privateInfo.setFaceRecordId(record.getId());
                    clothingInfoService.update(privateInfo);
                    isLend3 = true;
                }
                if (privateInfo.getCategory() == 4 && !isLend4) {
                    privateInfo.setStatus(9);
                    privateInfo.setOpUserId(record.getUserId());
                    privateInfo.setFaceRecordId(record.getId());
                    clothingInfoService.update(privateInfo);
                    isLend4 = true;
                }
            }

            //进行普通分配锁定，进行型号判断
            for (ClothingInfoEntity clothing: clothingList) {
                if (clothing.getSpecId().equals(specId1) && clothing.getCategory() == 1 && !isLend1) {
                    clothing.setStatus(9);
                    clothing.setOpUserId(record.getUserId());
                    clothing.setFaceRecordId(record.getId());
                    clothingInfoService.update(clothing);
                    isLend1 = true;
                }
                if (clothing.getSpecId().equals(specId2) && clothing.getCategory() == 2 && !isLend2) {
                    clothing.setStatus(9);
                    clothing.setOpUserId(record.getUserId());
                    clothing.setFaceRecordId(record.getId());
                    clothingInfoService.update(clothing);
                    isLend2 = true;
                }
                if (clothing.getSpecId().equals(specId3) && clothing.getCategory() == 3 && !isLend3) {
                    clothing.setStatus(9);
                    clothing.setOpUserId(record.getUserId());
                    clothing.setFaceRecordId(record.getId());
                    clothingInfoService.update(clothing);
                    isLend3 = true;
                }
                if (clothing.getSpecId().equals(specId4) && clothing.getCategory() == 4 && !isLend4) {
                    clothing.setStatus(9);
                    clothing.setOpUserId(record.getUserId());
                    clothing.setFaceRecordId(record.getId());
                    clothingInfoService.update(clothing);
                    isLend4 = true;
                }
            }
        }
        if (record.getType() == 2) {
            CargoEntity selectCargo = new CargoEntity();
            //selectCargo.setDepartmentId(record.getDepartmentId());
            selectCargo.setStatus(0);
            //查询普通货架
            selectCargo.setIsPrivate(0);
            List<CargoEntity> cargoOrdinaryList = cargoService.listByInfo(selectCargo);
            //查询专属货架
            selectCargo.setIsPrivate(1);
            List<CargoEntity> cargoExclusiveList = cargoService.listByInfo(selectCargo);

            //查询已借出
            ClothingInfoEntity selectClothing = new ClothingInfoEntity();
            selectClothing.setOpUserId(appUser.getId());
            selectClothing.setStatus(2);
            List<ClothingInfoEntity> clothingLendList = clothingList(selectClothing);
            Long cargoId = null;
            for (ClothingInfoEntity clothing: clothingLendList) {
                boolean isExclusive = false;
                for (CargoEntity cargo: cargoExclusiveList) {
                    if (cargo.getClothingId().equals(clothing.getId())) {
                        isExclusive = true;
                        cargo.setStatus(9);
                        cargo.setFaceRecordId(record.getId());
                        cargoService.update(cargo);
                        cargoId = cargo.getId();
                    }
                }
                if (!isExclusive) {
                    boolean isExclusive1 = false;
                    for (CargoEntity cargo: cargoOrdinaryList) {
                        if (!isExclusive1
                                && clothing.getCategory().equals(cargo.getCategory())
                                && clothing.getSeason().equals(cargo.getSeason())
                                //&& clothing.getType().equals(cargo.getType())
                                && cargo.getStatus() == 0) {
                            isExclusive1 = true;
                            cargo.setStatus(9);
                            cargo.setFaceRecordId(record.getId());
                            cargoService.update(cargo);
                            cargoId = cargo.getId();
                        }
                    }
                }
                if (cargoId == null) {
                    cargoId = 0L;
                }
                clothing.setStatus(9);
                clothing.setCargoId(cargoId);
                clothing.setFaceRecordId(record.getId());
                clothingInfoService.update(clothing);
            }
        }
    }

    @Override
    public ClothingInfoEntity distributionByRevert(ClothingInfoEntity info) {
        //查询普通货架，专属货架
        CargoEntity selectCargo1 = new CargoEntity();
        selectCargo1.setDepartmentId(info.getDepartmentId());
        selectCargo1.setStatus(0);
        selectCargo1.setIsPrivate(0);
        //selectCargo1.setType(info.getType());
        selectCargo1.setCategory(info.getCategory());
        selectCargo1.setSeason(info.getSeason());
        List<CargoEntity> cargoOrdinaryList = cargoService.listByInfo(selectCargo1);
        //查询专属货架
        CargoEntity selectCargo2 = new CargoEntity();
        selectCargo2.setStatus(0);
        selectCargo2.setIsPrivate(1);
        selectCargo2.setClothingId(info.getId());
        List<CargoEntity> cargoExclusiveList = cargoService.listByInfo(selectCargo2);
        Long cargoId = null;
        for (CargoEntity cargo: cargoExclusiveList) {
            if (cargoId == null) {
                cargo.setStatus(9);
                cargo.setFaceRecordId(0L);
                cargoService.update(cargo);
                cargoId = cargo.getId();
            }
        }
        if (cargoId == null) {
            for (CargoEntity cargo: cargoOrdinaryList) {
                if (cargoId == null) {
                    cargo.setStatus(9);
                    cargo.setFaceRecordId(0L);
                    cargoService.update(cargo);
                    cargoId = cargo.getId();
                }
            }
        }
        if (cargoId == null) {
            cargoId = 0L;
        }
        info.setStatus(9);
        info.setCargoId(cargoId);
        info.setFaceRecordId(0L);
        clothingInfoService.update(info);
        return info;
    }


    @Override
    public R upBatch(JSONObject requestParams) {
        String[] clothingIds = requestParams.getStr("clothingIds").split(",");
        CoolPreconditions.check(clothingIds.length == 0, "请识别被装");
        JSONObject tokenInfo = requestParams.getJSONObject("tokenInfo");
        BaseSysUserEntity sysUserEntity = sysUserService.getById(tokenInfo.get("userId").toString());
        Long[] ids = new Long[clothingIds.length];
        for (int i = 0; i < clothingIds.length; i++) {
            ids[i] = Long.parseLong(clothingIds[i]);
        }
        ClothingInfoEntity selectClothing = new ClothingInfoEntity();
        selectClothing.setIds(ids);
        List<ClothingInfoEntity> clothingInfoEntityList = clothingInfoService.listByInfo(selectClothing);

        List<CargoEntity> cargoList = new ArrayList<>();
        List<ClothingRecordEntity> recordList = new ArrayList<>();
        for (ClothingInfoEntity clothing: clothingInfoEntityList) {
            //修改状态
            if (clothing.getCargoId() != 0) {
                clothing.setStatus(1);
            } else {
                clothing.setStatus(0);
            }

            //创建记录
            ClothingRecordEntity clothingRecord = new ClothingRecordEntity();
            clothingRecord.setType(5);
            clothingRecord.setClothingId(clothing.getId());
            clothingRecord.setClothingCode(clothing.getCode());

            //更新货架
            if (clothing.getCargoId() != 0) {
                CargoEntity cargo = cargoService.getById(clothing.getCargoId());
                cargo.setStatus(1);
                cargoList.add(cargo);

                clothingRecord.setCargoId(cargo.getId());
                clothingRecord.setCargoPosition(cargo.getPosition());
            }

            clothingRecord.setSysUserId(sysUserEntity.getId());
            clothingRecord.setSysUserName(sysUserEntity.getName());
            clothingRecord.setDepartmentId(clothing.getDepartmentId());
            clothingRecord.setFaceRecordId(0L);
            clothingRecord.setStatus(1);
            clothingRecord.setCreateTime(new Date());
            recordList.add(clothingRecord);
        }
        cargoService.updateBatch(cargoList);
        clothingInfoService.updateBatch(clothingInfoEntityList);
        clothingRecordService.saveBatch(recordList);

        //解除锁定状态
        cancelByBatch(5);
        return R.ok();
    }

    @Override
    public R downBatch(JSONObject requestParams) {
        String[] clothingIds = requestParams.getStr("clothingIds").split(",");
        CoolPreconditions.check(clothingIds.length == 0, "请识别被装");

        JSONObject tokenInfo = requestParams.getJSONObject("tokenInfo");
        BaseSysUserEntity sysUserEntity = sysUserService.getById(tokenInfo.get("userId").toString());
        Long[] ids = new Long[clothingIds.length];
        for (int i = 0; i < clothingIds.length; i++) {
            ids[i] = Long.parseLong(clothingIds[i]);
        }
        ClothingInfoEntity selectClothing = new ClothingInfoEntity();
        selectClothing.setIds(ids);
        List<ClothingInfoEntity> clothingInfoEntityList = clothingInfoService.listByInfo(selectClothing);

        List<CargoEntity> cargoList = new ArrayList<>();
        List<ClothingRecordEntity> recordList = new ArrayList<>();
        for (ClothingInfoEntity clothing: clothingInfoEntityList) {
            //修改状态
            clothing.setStatus(0);
            //更新货架
            CargoEntity cargo = cargoService.getById(clothing.getCargoId());
            if (ObjectUtil.isNotEmpty(cargo)) {
                cargo.setStatus(0);
                cargoList.add(cargo);
            }

            //创建记录
            ClothingRecordEntity clothingRecord = new ClothingRecordEntity();
            clothingRecord.setType(6);
            clothingRecord.setClothingId(clothing.getId());
            clothingRecord.setClothingCode(clothing.getCode());
            if (ObjectUtil.isNotEmpty(cargo)) {
                clothingRecord.setCargoId(cargo.getId());
                clothingRecord.setCargoPosition(cargo.getPosition());
            }
            clothingRecord.setSysUserId(sysUserEntity.getId());
            clothingRecord.setSysUserName(sysUserEntity.getName());
            clothingRecord.setDepartmentId(clothing.getDepartmentId());
            clothingRecord.setFaceRecordId(0L);
            clothingRecord.setStatus(1);
            clothingRecord.setCreateTime(new Date());
            recordList.add(clothingRecord);
        }
        cargoService.updateBatch(cargoList);
        clothingInfoService.updateBatch(clothingInfoEntityList);
        clothingRecordService.saveBatch(recordList);
        return R.ok();
    }

    @Override
    public R lendBatch(JSONObject requestParams) {
        String[] clothingIds = requestParams.getStr("clothingIds").split(",");
        String people = requestParams.getStr("people");
        String mobile = requestParams.getStr("mobile");
        String org = requestParams.getStr("org");
        String approver = requestParams.getStr("approver");
        String operator = requestParams.getStr("operator");
        CoolPreconditions.check(StrUtil.isEmpty(org), "缺少借出机构");
        CoolPreconditions.check(StrUtil.isEmpty(people), "缺少借出人");
        CoolPreconditions.check(StrUtil.isEmpty(operator), "缺少经办人");
        CoolPreconditions.check(StrUtil.isEmpty(approver), "缺少审批人");
        CoolPreconditions.check(clothingIds.length == 0, "请识别被装");

        JSONObject tokenInfo = requestParams.getJSONObject("tokenInfo");
        BaseSysUserEntity sysUserEntity = sysUserService.getById(tokenInfo.get("userId").toString());

        //创建批量记录
        ClothingBatchEntity batchEntity = new ClothingBatchEntity();
        batchEntity.setCode(CodeUtils.batchCode());
        batchEntity.setPeople(people);
        batchEntity.setMobile(mobile);
        batchEntity.setOrg(org);
        batchEntity.setOperator(operator);
        batchEntity.setApprover(approver);
        batchEntity.setSysUserId(sysUserEntity.getId());
        batchEntity.setStatus(0);
        clothingBatchService.save(batchEntity);

        Long[] ids = new Long[clothingIds.length];
        for (int i = 0; i < clothingIds.length; i++) {
            ids[i] = Long.parseLong(clothingIds[i]);
        }
        ClothingInfoEntity selectClothing = new ClothingInfoEntity();
        selectClothing.setIds(ids);
        List<ClothingInfoEntity> clothingInfoEntityList = clothingInfoService.listByInfo(selectClothing);

        List<CargoEntity> cargoList = new ArrayList<>();
        List<ClothingRecordEntity> recordList = new ArrayList<>();
        List<ClothingBatchItemEntity> itemList = new ArrayList<>();
        for (ClothingInfoEntity clothing: clothingInfoEntityList) {
            //修改状态
            clothing.setStatus(4);
            //更新货架
            CargoEntity cargo = cargoService.getById(clothing.getCargoId());
            cargo.setStatus(0);
            cargoList.add(cargo);
            //创建记录
            ClothingRecordEntity clothingRecord = new ClothingRecordEntity();
            clothingRecord.setBatchId(batchEntity.getId());
            clothingRecord.setType(7);
            clothingRecord.setOrgAndPeople(org.concat("/").concat(people));
            clothingRecord.setOperator(operator);
            clothingRecord.setClothingId(clothing.getId());
            clothingRecord.setClothingCode(clothing.getCode());
            clothingRecord.setCargoId(cargo.getId());
            clothingRecord.setCargoPosition(cargo.getPosition());
            clothingRecord.setSysUserId(sysUserEntity.getId());
            clothingRecord.setSysUserName(sysUserEntity.getName());
            clothingRecord.setDepartmentId(clothing.getDepartmentId());
            clothingRecord.setFaceRecordId(0L);
            clothingRecord.setStatus(1);
            clothingRecord.setCreateTime(new Date());
            recordList.add(clothingRecord);

            ClothingBatchItemEntity itemEntity = new ClothingBatchItemEntity();
            itemEntity.setBatchId(batchEntity.getId());
            itemEntity.setClothingId(clothing.getId());
            itemEntity.setStatus(0);
            itemList.add(itemEntity);

        }
        cargoService.updateBatch(cargoList);
        clothingInfoService.updateBatch(clothingInfoEntityList);
        clothingRecordService.saveBatch(recordList);
        clothingBatchItemService.saveBatch(itemList);
        return R.ok();
    }

    @Override
    public R revertBatch(JSONObject requestParams) {
        String clothingIdStr = requestParams.getStr("clothingIds");
        String damageIdStr = requestParams.getStr("damageIds");
        String people = requestParams.getStr("people");
        String operator = requestParams.getStr("operator");
        Long batchId = requestParams.getLong("batchId");
        CoolPreconditions.check(StrUtil.isEmpty(people), "缺少归还人");
        CoolPreconditions.check(StrUtil.isEmpty(operator), "缺少经办人人");
        JSONObject tokenInfo = requestParams.getJSONObject("tokenInfo");
        BaseSysUserEntity sysUserEntity = sysUserService.getById(tokenInfo.get("userId").toString());
        String[] clothingIds = new String[0];
        if (StrUtil.isNotEmpty(clothingIdStr)) {
            clothingIds = clothingIdStr.split(",");
        }
        String[] damageIds = new String[0];
        if (StrUtil.isNotEmpty(damageIdStr)) {
            damageIds = damageIdStr.split(",");
        }
        CoolPreconditions.check(clothingIds.length + damageIds.length == 0, "请识别被装");
        //查询被装数据
        Long[] ids = new Long[clothingIds.length];
        for (int i = 0; i < clothingIds.length; i++) {
            ids[i] = Long.parseLong(clothingIds[i]);
        }
        Long[] dIds = new Long[damageIds.length];
        for (int i = 0; i < damageIds.length; i++) {
            dIds[i] = Long.parseLong(damageIds[i]);
        }
        ClothingInfoEntity selectClothing = new ClothingInfoEntity();
        selectClothing.setIds(ids);
        List<ClothingInfoEntity> clothingInfoEntityList = new ArrayList<>();
        if (ids.length >0) {
            clothingInfoEntityList = clothingInfoService.listByInfo(selectClothing);
        }
        selectClothing.setIds(dIds);
        List<ClothingInfoEntity> damageList = new ArrayList<>();
        if (dIds.length > 0) {
            damageList = clothingInfoService.listByInfo(selectClothing);
        }

        //查询批量表
        ClothingBatchEntity batchEntity = clothingBatchService.getById(batchId);
        List<CargoEntity> cargoList = new ArrayList<>();
        List<ClothingRecordEntity> recordList = new ArrayList<>();
        List<ClothingBatchItemEntity> itemList = new ArrayList<>();

        for (ClothingInfoEntity clothing: clothingInfoEntityList) {
            //修改状态
            if (clothing.getCargoId() != 0) {
                clothing.setStatus(1);
            } else {
                clothing.setStatus(0);
            }

            //创建记录
            ClothingRecordEntity clothingRecord = new ClothingRecordEntity();
            clothingRecord.setType(8);
            clothingRecord.setOrgAndPeople(batchEntity.getOrg().concat("/").concat(people));
            clothingRecord.setOperator(operator);
            clothingRecord.setClothingId(clothing.getId());
            clothingRecord.setClothingCode(clothing.getCode());

            //更新货架
            if (clothing.getCargoId() != 0) {
                CargoEntity cargo = cargoService.getById(clothing.getCargoId());
                cargo.setStatus(1);
                cargoList.add(cargo);

                clothingRecord.setCargoId(cargo.getId());
                clothingRecord.setCargoPosition(cargo.getPosition());
            }

            clothingRecord.setSysUserId(sysUserEntity.getId());
            clothingRecord.setSysUserName(sysUserEntity.getName());
            clothingRecord.setDepartmentId(clothing.getDepartmentId());
            clothingRecord.setFaceRecordId(0L);
            clothingRecord.setStatus(1);
            clothingRecord.setCreateTime(new Date());
            recordList.add(clothingRecord);

            ClothingBatchItemEntity selectItem = new ClothingBatchItemEntity();
            selectItem.setClothingId(clothing.getId());
            selectItem.setBatchId(batchId);
            selectItem.setStatus(0);
            List<ClothingBatchItemEntity> item1List = clothingBatchItemService.listByInfo(selectItem);
            for (ClothingBatchItemEntity item1: item1List) {
                item1.setStatus(1);
                item1.setPeople(people);
                item1.setOperator(operator);
                itemList.add(item1);
            }
        }
        for (ClothingInfoEntity clothing: damageList) {
            clothing.setStatus(3);
            //创建记录
            ClothingRecordEntity clothingRecord = new ClothingRecordEntity();
            clothingRecord.setType(8);
            clothingRecord.setOrgAndPeople(batchEntity.getOrg().concat("/").concat(people));
            clothingRecord.setOperator(operator);
            clothingRecord.setClothingId(clothing.getId());
            clothingRecord.setClothingCode(clothing.getCode());
            clothingRecord.setSysUserId(sysUserEntity.getId());
            clothingRecord.setSysUserName(sysUserEntity.getName());
            clothingRecord.setDepartmentId(clothing.getDepartmentId());
            clothingRecord.setFaceRecordId(0L);
            clothingRecord.setStatus(0);
            clothingRecord.setCreateTime(new Date());
            recordList.add(clothingRecord);

            ClothingBatchItemEntity selectItem = new ClothingBatchItemEntity();
            selectItem.setClothingId(clothing.getId());
            selectItem.setBatchId(batchId);
            selectItem.setStatus(0);
            List<ClothingBatchItemEntity> item1List = clothingBatchItemService.listByInfo(selectItem);
            for (ClothingBatchItemEntity item1: item1List) {
                item1.setStatus(2);
                item1.setPeople(people);
                item1.setOperator(operator);
                itemList.add(item1);
            }
        }

        //保存归还经办人，和归还人信息
        if (StrUtil.isNotEmpty(batchEntity.getRevertPeoples())) {
            String[] revertPeoples=batchEntity.getRevertPeoples().split(",");
            List<String> peopleList = new ArrayList<>();
            boolean isAdd = true;
            for (String str: revertPeoples) {
                if (str.equals(people)) {
                    isAdd = false;
                }
                peopleList.add(str);
            }
            if (isAdd) {
                peopleList.add(people);
                batchEntity.setRevertPeoples(String.join(",", peopleList));
            }
        } else {
            batchEntity.setRevertPeoples(people);
        }

        if (StrUtil.isNotEmpty(batchEntity.getRevertOperators())) {
            String[] revertOperators=batchEntity.getRevertOperators().split(",");
            List<String> operatorList = new ArrayList<>();
            boolean isAdd = true;
            for (String str: revertOperators) {
                if (str.equals(operator)) {
                    isAdd = false;
                }
                operatorList.add(str);
            }
            if (isAdd) {
                operatorList.add(operator);
                batchEntity.setRevertOperators(String.join(",", operatorList));
            }
        } else {
            batchEntity.setRevertOperators(operator);
        }
        clothingBatchService.updateById(batchEntity);

        cargoService.updateBatch(cargoList);
        clothingInfoService.updateBatch(clothingInfoEntityList);
        clothingInfoService.updateBatch(damageList);
        clothingRecordService.saveBatch(recordList);
        clothingBatchItemService.updateBatch(itemList);
        //解除锁定状态
        cancelByBatch(2);
        //根据修改的item修改状态
        clothingBatchService.updateStatusByItem(itemList);
        return R.ok();
    }

    public List<ClothingInfoEntity> clothingList(ClothingInfoEntity selectInfo) {
        ClothingInfoEntity selectInfo1 = new ClothingInfoEntity();
        selectInfo1.setCategory(1);
        ClothingInfoEntity selectInfo2 = new ClothingInfoEntity();
        selectInfo2.setCategory(2);
        ClothingInfoEntity selectInfo3 = new ClothingInfoEntity();
        selectInfo3.setCategory(3);
        ClothingInfoEntity selectInfo4 = new ClothingInfoEntity();
        selectInfo4.setCategory(4);

        if (selectInfo.getSeason() != null) {
            selectInfo1.setSeason(selectInfo.getSeason());
            selectInfo2.setSeason(selectInfo.getSeason());
        }
        if (selectInfo.getStatus() != null) {
            selectInfo1.setStatus(selectInfo.getStatus());
            selectInfo2.setStatus(selectInfo.getStatus());
            selectInfo3.setStatus(selectInfo.getStatus());
            selectInfo4.setStatus(selectInfo.getStatus());
        }

        if (selectInfo.getFaceRecordId() != null) {
            selectInfo1.setFaceRecordId(selectInfo.getFaceRecordId());
            selectInfo2.setFaceRecordId(selectInfo.getFaceRecordId());
            selectInfo3.setFaceRecordId(selectInfo.getFaceRecordId());
            selectInfo4.setFaceRecordId(selectInfo.getFaceRecordId());
        }
        if (selectInfo.getIsPrivate() != null) {
            selectInfo1.setIsPrivate(selectInfo.getIsPrivate());
            selectInfo2.setIsPrivate(selectInfo.getIsPrivate());
            selectInfo3.setIsPrivate(selectInfo.getIsPrivate());
            selectInfo4.setIsPrivate(selectInfo.getIsPrivate());
        }
        if (selectInfo.getOpUserId() != null) {
            selectInfo1.setOpUserId(selectInfo.getOpUserId());
            selectInfo2.setOpUserId(selectInfo.getOpUserId());
            selectInfo3.setOpUserId(selectInfo.getOpUserId());
            selectInfo4.setOpUserId(selectInfo.getOpUserId());
        }
        if (selectInfo.getPrUserId() != null) {
            selectInfo1.setPrUserId(selectInfo.getPrUserId());
            selectInfo2.setPrUserId(selectInfo.getPrUserId());
            selectInfo3.setPrUserId(selectInfo.getPrUserId());
            selectInfo4.setPrUserId(selectInfo.getPrUserId());
        }
        if (selectInfo.getDepartmentId() != null) {
            selectInfo1.setDepartmentId(selectInfo.getDepartmentId());
            selectInfo2.setDepartmentId(selectInfo.getDepartmentId());
            selectInfo3.setDepartmentId(selectInfo.getDepartmentId());
            selectInfo4.setDepartmentId(selectInfo.getDepartmentId());
        }
        List<ClothingInfoEntity> list = new ArrayList<>();
        List<ClothingInfoEntity> list1 = clothingInfoService.listByInfo(selectInfo1);
        List<ClothingInfoEntity> list2 = clothingInfoService.listByInfo(selectInfo2);
        List<ClothingInfoEntity> list3 = clothingInfoService.listByInfo(selectInfo3);
        List<ClothingInfoEntity> list4 = clothingInfoService.listByInfo(selectInfo4);
        list.addAll(list1);
        list.addAll(list2);
        list.addAll(list3);
        list.addAll(list4);
        return list;
    }

    public void cancelByBatch(int type) {
        //type=2,批量外借归还，type=5 上架
        ClothingInfoEntity selectClothing = new ClothingInfoEntity();
        selectClothing.setStatus(9);
        selectClothing.setFaceRecordId(0L);
        CargoEntity selectCargo = new CargoEntity();
        selectCargo.setStatus(9);
        selectCargo.setFaceRecordId(0L);
        List<ClothingInfoEntity> clothingLockList = clothingInfoService.listByInfo(selectClothing);
        List<CargoEntity> cargoLockList = cargoService.listByInfo(selectCargo);
        for (ClothingInfoEntity clothing: clothingLockList) {
            if (type == 2) {
                clothing.setStatus(4);
            } else {
                clothing.setStatus(0);
            }
        }
        for (CargoEntity cargo: cargoLockList) {
            cargo.setStatus(0);
        }
        clothingInfoService.updateBatch(clothingLockList);
        cargoService.updateBatch(cargoLockList);
    }
}